/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef SLE_UPDATE_PARAM_MANAGER_H
#define SLE_UPDATE_PARAM_MANAGER_H

#include <string>
#include <memory>
#include <map>
#include <mutex>
#include "sle_connect_config_manager.h"
#include "sle_seek_config_manager.h"
#include "sle_announce_config_manager.h"
#include "sle_common_config.h"
#include "sle_error_code.h"
#include "sle_ssap_config.h"
#include "sle_address_parcel.h"
#include "sle_connect_parcel.h"
#include "sle_event_handler.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {

class AdvancedSettingManager {
public:
    AdvancedSettingManager();
    ~AdvancedSettingManager();
    static AdvancedSettingManager *GetInstance();
    void InitAdvancedSettingManager();
    SleErrorCode UpdateAllConnectParam(std::vector<SleConnectDevice> &successUpdateDev);
    SleErrorCode GetRecomSendDataSize(uint16_t connectId, uint32_t &recomSendDataSize);
    SleErrorCode GetHostDefaultConnectConfig(SleConnectConfig &connParamConfig);
    SleErrorCode GetDefaultSeekParamConfig(SleSeekParam &seekParam);
    SleErrorCode GetDefaultAnnounceParamConfig(SleAnnounceParam &announceConfig);

public:
    SleErrorCode ASProcessSleStateChange(SleDeviceState state);//host-state
    SleErrorCode ASProcessConnectStateChange(uint16_t connectId, const SleAddress &address, SleConnectState state,
        SlePairState pairState, SleDisConnectReason reason);//connect-state
    SleErrorCode ASProcessConnParamUpdateComplete(uint16_t connectId, const SleConnectionParamUpdateEvt &param,
        SleErrorCode errCode);//connect-update
    SleErrorCode ASProcessSsapcExchangeInfo(uint8_t clientId, uint16_t connectId, const SsapMtuInfo &info,
        SleErrorCode errCode);//client-exchange-mtu
    SleErrorCode ASProcessSsapsMtuInfoChanged(uint8_t serverId, uint16_t connectId, const SsapMtuInfo &info,
        SleErrorCode errCode);//server-exchange-mtu

private:
    uint32_t ConvertMtuToDataSize(uint32_t mtuSize);
    uint16_t CalculateConnInterval(uint16_t stepCount, uint16_t stepPeriod, uint16_t devNum);
    SleErrorCode SetSendDataSize(uint16_t connectId, uint32_t mtu);
    SleErrorCode CalculateConnParam(SleConnectParam &param);
    SleErrorCode GetConnectedDeviceList(std::vector<SleConnectDevice> &devices, uint16_t &number);
    SleErrorCode GetDefaultConnParam(SleDefaultConnectionParam &defaultConnParam);
    SleErrorCode SetDefaultConnParam();
    void RemoveRecomSendDataToMap(uint16_t connectId);

private:
    std::mutex sizeLock_;
    std::map<uint16_t, uint32_t> packageLenMap_; //connectId -- sendDataSize
    std::shared_ptr<AppExecFwk::EventRunner> advancedEventRunner_ = nullptr;
    std::shared_ptr<SleEventHandler> advancedEventHandler_  = nullptr;
    std::unique_ptr<SleConnectConfigManager> connCfgMgr_ = nullptr;
    std::unique_ptr<SleAnnounceConfigManager> announceCfgMgr_ = nullptr;
    std::unique_ptr<SleSeekConfigManager> seekCfgMgr_ = nullptr;
    SleConnectConfig connectConfig_;
    SleConnectModeConfig connectModeConfig_;
    SleDefaultConnectionParam defaultConfig_;
    SleAnnounceParam announceConfig_;
    SleSeekParam seekConfig_;

};

}
}
}

#endif //SLE_UPDATE_PARAM_MANAGER_H
